/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#include "pch.h"
#include <journal/models/page_model_factory.h>
#include <journal/models/notebook_model.h>

using namespace journal::model;

namespace bt = baja::transition_tracking;

void notebook_model::undoChildAction(
    const std::vector<bt::transition_path_node>& path,
    const b::uint32& childIndex,
    const std::shared_ptr<bt::itransition_parameters>& transitionParameters
    )
{
    DEBUG_CONSOLE_TRACE_TAG(TRACE_TAG_UNDO_REDO,
        boost::wformat(L"notebook_model::undoChildAction() : model id = %i") %
        genericIdentity()
        );

    // check if it is a page...
    if (path[childIndex].typeToken == _pageFactory->typeToken())
    {
        b::uint32 pageId = path[childIndex].id;

        // it is a page, find the specific page...
        for (auto &iter : _pages)
        {
            if (iter->storageId() == pageId)
            {
                std::shared_ptr<bt::itransition_target> page = std::dynamic_pointer_cast<bt::itransition_target>(iter);
                page->undo(path, childIndex, transitionParameters);
                break;
            }
        }
    }
}

void notebook_model::redoChildAction(
    const std::vector<bt::transition_path_node>& path,
    const b::uint32& childIndex,
    const std::shared_ptr<bt::itransition_parameters>& transitionParameters
    )
{
    DEBUG_CONSOLE_TRACE_TAG(TRACE_TAG_UNDO_REDO,
        boost::wformat(L"notebook_model::redoChildAction() : model id = %i") %
        genericIdentity()
        );

    // check if it is a page...
    if (path[childIndex].typeToken == _pageFactory->typeToken())
    {
        b::uint32 pageId = path[childIndex].id;

        // it is a page, find the specific page...
        for (auto &iter : _pages)
        {
            if (iter->storageId() == pageId)
            {
                std::shared_ptr<bt::itransition_target> page = std::dynamic_pointer_cast<bt::itransition_target>(iter);
                page->redo(path, childIndex, transitionParameters);
                break;
            }
        }
    }
}

